/* FlowPlayer Server Control for ASP.NET 2.0
 * Copyright (C) 2008 Gaiaware AS
 * All rights reserved. */

if( !Gaia.Extensions )
  Gaia.Extensions = Class.create();

Gaia.Extensions.FlowPlayer = Class.create();

/* inherit from Gaia Control*/
Object.extend(Gaia.Extensions.FlowPlayer.prototype, Gaia.Control.prototype);

/* implementation */
Object.extend(Gaia.Extensions.FlowPlayer.prototype, {

  // "Constructor"
  initialize: function(element, options){
    this.initializeFlowPlayer(element, options);
  },

  initializeFlowPlayer: function(element, options){
    this.initializeControl(element, options);
    
    // initialize the flash variables 
    var flashVars = {
			src:this.options.flowPlayerSrc,
			width: this.options.width, 
			height: this.options.height,
			menu: this.options.showFlashMenu,
			wmode: 'transparent'
		};
          
    // event hooks
    if (options.listenToStateChanges) {
      window.onPlay = function(e) { this._dispatchStateChanged('Playing'); }.bind(this);
      window.onStop = function(e) { this._dispatchStateChanged('Stopped'); }.bind(this);
      window.onPause = function(e) { this._dispatchStateChanged('Paused'); }.bind(this);
      window.onResume = function(e) { this._dispatchStateChanged('Resumed'); }.bind(this);
    }

    // Initial CuePoints are added after the FlowPlayer is ready if the video has cuePoints
    window.onFlowPlayerReady = function(e) { 
      if (this.options.cuePoints) 
        this.player.addCuePoints(this.options.cuePoints); 
    }.bind(this);

    window.onCuePoint = function(cuePoint) { this._dispatchCuePoint(cuePoint); }.bind(this); // cuepoint reached
    
    // listen to clip done events? 
    window.onClipDone = function(clip) {
      if (this.options.listenToClipDone)
        this._dispatchClipDone(clip);
    }.bind(this);
    
    // initialize flowplayer
    this.player = flashembed(this.element.id + '_object', flashVars, this.options.cfg);
        
  },
   
  // dispatches an ajax request to the control updating it's state
  _dispatchStateChanged : function(state){
    Gaia.Control.callControlMethod.bind(this)('PlayStatusChangedMethod', [state], null , this.element.id);
  },
  
  _dispatchCuePoint: function(cue) {
    Gaia.Control.callControlMethod.bind(this)('CuePointReachedMethod', [cue.name, cue.time], null , this.element.id);
  },
  
  _dispatchClipDone: function(clip) {
    Gaia.Control.callControlMethod.bind(this)('VideoClipFinishedMethod', null, null , this.element.id);
  }, 
 
  updatePlayer: function(cuePoints, cfg){
	this.options.cuePoints = cuePoints;
	this.player.setConfig(cfg.config);
	
	if (cuePoints)
	  this.player.addCuePoints(this.options.cuePoints);
  }, 
  
  setPlay: function(value) {
    this.player.DoPlay();
    return this;
  },
  
  setStop : function(value) {
    this.player.DoStop();
    return this;
  },
  
  setReset : function(value) {
    this.player.Reset();
    return this;
  },
  
  setPause : function(value) {
    this.player.Pause();
    return this;
  },
  
  setSeek : function(value) {
    this.player.Seek(value);
    return this;
  },
    
  destroy : function(){
    
    // call base destroy
    this._destroyImpl();
  }

});

Gaia.Extensions.FlowPlayer.browserFinishedLoading = true;